﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class UIResetUserData : MonoBehaviour 
{
    private Image img_Bg;
    private Color img_BgColor;
    private Transform image;
    private Button btn_No;
    private Button btn_Yes;

    private void Awake()
    {
        Init();
    }

    /// <summary>
    /// 初始化UIUserData
    /// </summary>
    private void Init()
    {
        EventCenter.AddListener(CL.EventType.ShowUIResetUserData, ShowUIResetUserData);
        EventCenter.AddListener(CL.EventType.HideUIResetUserData, HideUIResetUserData);

        img_Bg = transform.Find("Bg").GetComponent<Image>();
        img_BgColor = img_Bg.color;
        image = transform.Find("Image");
        image.localScale = Vector3.one;
        btn_No = transform.Find("Image/Btn_No").GetComponent<Button>();
        btn_No.onClick.AddListener(Btn_No);
        btn_Yes = transform.Find("Image/Btn_Yes").GetComponent<Button>();
        btn_Yes.onClick.AddListener(Btn_Yes);


        gameObject.SetActive(false);// DOTween动画在UIMainOpen第一次执行时露馅了。。。
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener(CL.EventType.ShowUIResetUserData, ShowUIResetUserData);
        EventCenter.RemoveListener(CL.EventType.HideUIResetUserData, HideUIResetUserData);
    }


    /// <summary>
    /// 显示、隐藏UIResetUserData
    /// </summary>
    private void ShowUIResetUserData()
    {
        gameObject.SetActive(true);
        img_BgColor = Color.clear;
        img_Bg.DOColor(img_BgColor, 0.3f);
        image.localScale = Vector3.one;
        image.DOScale(Vector3.one, 0.3f);
    }
    private void HideUIResetUserData()
    {
        img_Bg.DOColor(Color.clear, 0.3f);
        image.DOScale(Vector3.zero, 0.3f).OnComplete(() => { gameObject.SetActive(false); });
    }


    /// <summary>
    /// 否按钮点击
    /// </summary>
    private void Btn_No()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIResetUserDataClose();
    }
    /// <summary>
    /// 是按钮点击
    /// </summary>
    private void Btn_Yes()
    {
        AudioManager.Instance.PlaySound("button");
        //不走动画，直接关闭
        gameObject.SetActive(false);
        DataManager.Instance.ResetUserData();
    }
}
